//分割等和⼦集（medium）: https://leetcode.cn/problems/partition-equal-subset-sum/
class Solution
{
public:
	bool canPartition(vector<int>& nums)
	{
		int n = nums.size(), sum = 0;
		for (auto x : nums)
		{
			sum += x;
		}
		if (sum % 2) 
		{
			return false;
		}
		
		// 如果不能平分，直接返回 false

		int aim = sum / 2; // 定义⼀下⽬标值
		vector<vector<bool>> dp(n + 1, vector<bool>(aim + 1)); // 建表
		for (int i = 0; i <= n; i++)
		{
			dp[i][0] = true; // 初始化
		}
		for (int i = 1; i <= n; i++) // 填表
		{
			for (int j = 1; j <= aim; j++)
			{
				dp[i][j] = dp[i - 1][j];
				if (j >= nums[i - 1])
					dp[i][j] = dp[i][j] || dp[i - 1][j - nums[i - 1]];
			}
		}
			
		// 返回结果
		return dp[n][aim];
	}
};


//⽬标和（medium）: https://leetcode.cn/problems/target-sum/
class Solution
{
public:
	int findTargetSumWays(vector<int>& nums, int target)
	{
		int sum = 0;
		for (auto x : nums)
		{
			sum += x;
		}
		int aim = (sum + target) / 2;
		// 处理⼀下边界条件
		if (aim < 0 || (sum + target) % 2)
		{
			return 0;
		}
		int n = nums.size();

		vector<vector<int>> dp(n + 1, vector<int>(aim + 1)); // 建表
		dp[0][0] = 1; // 初始化
		for (int i = 1; i <= n; i++) // 填表
		{
			for (int j = 0; j <= aim; j++)
			{
				dp[i][j] = dp[i - 1][j];
				if (j >= nums[i - 1])
				{
					dp[i][j] += dp[i - 1][j - nums[i - 1]];
				}
			}
		}
			
		// 返回结果
		return dp[n][aim];
	}
};